package com.example.demo.data

import org.hibernate.validator.constraints.CreditCardNumber
import org.jetbrains.annotations.NotNull
import org.springframework.data.rest.core.annotation.RestResource
import org.springframework.hateoas.RepresentationModel
import java.io.Serializable
import java.net.UnknownServiceException
import java.sql.Date
import java.util.*
import javax.persistence.*
import javax.validation.constraints.Digits
import javax.validation.constraints.NotBlank
import javax.validation.constraints.Pattern
import javax.validation.constraints.Size

@Entity
@RestResource(rel = "tacos", path = "tacos")
data class Taco(
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        var id: Long = -1,
        var createdAt: java.util.Date = java.util.Date(),
        @field:NotNull
        @field:Size(min = 5, message = "Name must be at least 5 characters long")
        var name: String = "",
        //TODO: 这里如果开启的话,无法通过检查,具体原因还未知
//        @field:Size(min = 1, message = "You must be choose at lease 1 ingredient")
        @ElementCollection
        var ingredients: List<String> = mutableListOf()
) {

    @PrePersist
    fun createdAt() {
        this.createdAt = java.util.Date()
    }
}

@Entity
data class Ingredient(
        @Id
        val id: String,
        val name: String,
        @Enumerated(EnumType.STRING)
        val type: Type
) {
    enum class Type {
        WRAP, PROTEIN, VEGGIES, CHEESE, SAUCE;
    }
}

@Entity
@Table(name = "Taco_Order")
data class Order(
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        var id: Long? = null,
        var placedAt: Date = Date(System.currentTimeMillis()),
        @field:NotBlank(message = "Name is required")
        var deliveryName: String? = "",
        @field:NotBlank(message = "street is required")
        var deliveryStreet: String? = "",
        @field:NotBlank(message = "city is required")
        var deliveryCity: String? = "",
        @field:NotBlank(message = "state is required")
        var deliveryState: String? = "",
        @field:NotBlank(message = "zip is required")
        var deliveryZip: String? = "",
        @field:CreditCardNumber()
        var ccNumber: String? = "",
        @field:Pattern(regexp="^(0[1-9]|1[0-2])([\\/])([1-9][0-9])$",
                message="Must be formatted MM/YY")
        var ccExpiration: String? = "",
        @field:Digits(integer = 3, fraction = 0, message = "Invalid CVV")
        var ccCVV: String? = "",
        @ManyToMany(targetEntity = Taco::class)
        var tacos: MutableList<Taco> = ArrayList(),
        @ManyToOne
        var user: User? = null
): Serializable {

    @PrePersist
    fun createdAt() {
        this.placedAt = Date(System.currentTimeMillis())
    }

    fun addDesign(design: Taco) {
        tacos.add(design)
    }
}